package com.sunmouse.toolkit.ftp;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/23.
 */
public class FTPHelper {
    private FTPClient mFTPClient;
    private Context context;
    private static FTPHelper mFtp;
    private OnFTPListener mListener;
    private Handler mHandler;

    private FTPHelper(Context context) {
        this.mFTPClient = new FTPClient();
        this.context = context;
        this.mHandler = new FTPHandler();
    }


    public void setListener(OnFTPListener mListener) {
        this.mListener = mListener;
    }

    /**
     * ftp helper factory
     */
    public static FTPHelper getInstance(Context context) {
        if (mFtp == null) {

            mFtp = new FTPHelper(context);
        }
        return mFtp;
    }

    /**
     * connect to service
     *
     * @param ip
     * @param port
     * @param userName
     * @param psw
     */
    public void connect(String ip, int port, String userName, String psw) {
        try {
            mFTPClient.connect(ip, port);
            mHandler.sendEmptyMessage(mFTPClient.getReplyCode());
//            LogUtils.d("----------" + mFTPClient.getReplyCode());
            mFTPClient.login(userName, psw);
            mFTPClient.enterLocalPassiveMode();//passive mode
            mFTPClient.setControlKeepAliveTimeout(2 * 1000);
            mHandler.sendEmptyMessage(mFTPClient.getReplyCode());
//            LogUtils.d("----------" + mFTPClient.getReplyCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<FileBean> getFiles() {
        ArrayList<FileBean> outList = new ArrayList<>();

        if (!mFTPClient.isConnected()) {
            return outList;
        }
        try {
            FTPFile[] ftpFile = mFTPClient.listFiles();
            if (ftpFile == null) return outList;
            for (FTPFile f : ftpFile) {
                FileBean b = new FileBean();
                b.setName(f.getName());
                b.setFileType(f.getType());
                b.setParent("root");
                b.setSize(f.getSize());
                outList.add(b);
            }
//            LogUtils.d("---ftp---:name=" + f.getName() + "group=" + f.getGroup());
//            LogUtils.d("---ftp---:subs.Size=" + subs.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outList;
    }

    /**
     * @param dir get files in this dir
     * @return
     */
    public ArrayList<FileBean> getFiles(String dir) {
        ArrayList<FileBean> outList = new ArrayList<>();
        try {
            FTPFile[] subs = mFTPClient.listFiles(dir);//get sub files in this directory
            if (subs == null) return outList;
            for (FTPFile f : subs) {
                FileBean b = new FileBean();
                b.setParent(dir);
                b.setName(f.getName());
                b.setSize(f.getSize());
                b.setFileType(f.getType());
                outList.add(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outList;
    }

    /**
     * return client
     */
    public FTPClient getClient() {
        return mFTPClient;
    }


    /**
     * disconnect
     */
    public void disconnet() {
        if (mFTPClient != null) {
            try {
                mFTPClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class FTPHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mListener == null) return;
            switch (msg.what) {
                case FTPReply.SERVICE_READY:
                    mListener.onReady();
                    break;
                case FTPReply.USER_LOGGED_IN:
                    mListener.onLogin();
                    break;
                default:
                    mListener.onFailure();
                    break;
            }
        }
    }


}
